package com.loongtech.bi.action.flow;

import com.loongtech.bi.action.Base2BiSearchAction;
import com.loongtech.bi.manager.flow.HadoopQueryManager;
import com.loongtech.bi.manager.system.SysLogManager;
import com.loongtech.bi.support.Session;
import com.loongtech.core.util.Time;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map.Entry;

@Controller
@Scope("view")
public class AuanyPayorderFlowAction extends Base2BiSearchAction {
	private static final long serialVersionUID = 1L;
	protected DecimalFormat df = new DecimalFormat("######0.00");
	protected Date dateStart = new Date(Time.getBeginningOfDay(Time
			.currentTime()));
	protected Date dateEnd = new Date(
			Time.getBeginningOfDay(Time.currentTime()));

	private AuanyPayorderFlow queryBean; // 查询bean

	private List<Object[]> result; // 结果List

	@Autowired
	HadoopQueryManager hadoopQueryManager;

	@Autowired
	private SysLogManager sysLogManager;
	@Autowired
	private Session session;

	@PostConstruct
	public void init() {
		queryBean = new AuanyPayorderFlow();
		result = new ArrayList<>();
	}

	public void search() {
		sysLogManager.addLog(session.getUserName(), "流水查询—支付订单流水");

		result = new ArrayList<>();

		FacesContext context = FacesContext.getCurrentInstance();

		// 开始结束时间不能大于当前时间60天
		if (Time.getInterval(new Date(), dateStart) > 60
				|| Time.getInterval(new Date(), dateEnd) > 60) {
			context.addMessage(null, new FacesMessage("提示",
					"开始结束时间不能小于当前时间60天!"));
			return;
		}

		// 全用户名，角色ID必须至少有一个非空!
		if (StringUtils.isBlank(queryBean.getFulluseridentity())
				&& StringUtils.isBlank(queryBean.getRoleid())) {
			context.addMessage(null, new FacesMessage("提示",
					"全用户名，角色ID必须至少有一个非空!"));
			return;
		}

		if (HadoopQueryManager.VISIT_QUEUE.offer("AuanyPayorderFlowAction")) {
			System.out.println("VISIT_QUEUE offer AuanyPayorderFlowAction ok!");

			StringBuffer sql = new StringBuffer();
			sql.append("select logtime,hostname,modulename,gameid,auanyserverid,logid,logtag,appid,deviceCategory,platname,fulluseridentity,serverid,roleid,gameproductid,gameproductnum,currency,amount,rawcurrency,rawamount,gameproductamount,gameOrderId,platOrderId,gamecurrencytype,gamecurrencynum,extragamecurrencynum,extra1,extra2 from auany_payorder where 1=1 ");
            if (!StringUtils.isBlank(queryBean.getRoleid())) {
                sql.append(" and roleid = '");
                sql.append(queryBean.getRoleid());
                sql.append("' ");
            }
			if (!"0".equals(queryServer)) {
				sql.append(" and zoneid = '");
				sql.append(queryServer);
				sql.append("' ");
			}
			if (queryBean.getAuanyserverid() != null
					&& !"".equals(queryBean.getAuanyserverid())) {
				sql.append(" and auanyserverid = '");
				sql.append(queryBean.getAuanyserverid().trim());
				sql.append("' ");
			}
			if (queryBean.getFulluseridentity() != null
					&& !"".equals(queryBean.getFulluseridentity())) {
				sql.append(" and fulluseridentity = '");
				sql.append(queryBean.getFulluseridentity().trim());
				sql.append("' ");
			}
			if (queryBean.getGameproductid() != null
					&& !"".equals(queryBean.getGameproductid())) {
				sql.append(" and gameproductid = '");
				sql.append(queryBean.getGameproductid().trim());
				sql.append("' ");
			}
			if (queryBean.getGameorderid() != null
					&& !"".equals(queryBean.getGameorderid())) {
				sql.append(" and gameorderid = '");
				sql.append(queryBean.getGameorderid().trim());
				sql.append("' ");
			}
			if (queryBean.getPlatorderid() != null
					&& !"".equals(queryBean.getPlatorderid())) {
				sql.append(" and platorderid = '");
				sql.append(queryBean.getPlatorderid().trim());
				sql.append("' ");
			}
            sql.append(" and dt >= '");
            sql.append(Time.getDateStr(dateStart));
            sql.append("' and dt <= '");
            sql.append(Time.getDateStr(dateEnd));
            sql.append("' ");
			sql.append(" order by logtime desc ");

			System.out.println(sql.toString());
			Entry<List<String>, List<Object[]>> ret = hadoopQueryManager
					.executeSQL(sql.toString());
			if (null == ret || null == ret.getKey() || ret.getKey().isEmpty()
					|| null == ret.getValue()) {
				context.addMessage(null, new FacesMessage("提示", "查询结果为空!"));
				return;
			}

			result = ret.getValue();
		} else {
			System.out
					.println("VISIT_QUEUE offer AuanyPayorderFlowAction failed!");
			context.addMessage(null, new FacesMessage("提示",
					"当前系统正被访问，为保证服务器稳定，请稍后再试"));
			return;
		}
	}

	public class AuanyPayorderFlow {
		public String dt;
		public String logtime;
		public String hostname;
		public String modulename;
		public String gameid;
		public String auanyserverid;
		public String logid;
		public String logtag;
		public String appid;
		public String deviceCategory;
		public String platname;
		public String fulluseridentity;
		public String serverid;
		public String roleid;
		public String gameproductid;
		public int gameproductnum;
		public String currency;
		public int amount;
		public String rawcurrency;
		public int rawamount;
		public int gameproductamount;
		public String gameorderid;
		public String platorderid;
		public int gamecurrencytype;
		public int gamecurrencynum;
		public int extragamecurrencynum;
		public String extra1;
		public String extra2;

		public String getDt() {
			return dt;
		}

		public void setDt(String dt) {
			this.dt = dt;
		}

		public String getLogtime() {
			return logtime;
		}

		public void setLogtime(String logtime) {
			this.logtime = logtime;
		}

		public String getHostname() {
			return hostname;
		}

		public void setHostname(String hostname) {
			this.hostname = hostname;
		}

		public String getModulename() {
			return modulename;
		}

		public void setModulename(String modulename) {
			this.modulename = modulename;
		}

		public String getGameid() {
			return gameid;
		}

		public void setGameid(String gameid) {
			this.gameid = gameid;
		}

		public String getAuanyserverid() {
			return auanyserverid;
		}

		public void setAuanyserverid(String auanyserverid) {
			this.auanyserverid = auanyserverid;
		}

		public String getLogid() {
			return logid;
		}

		public void setLogid(String logid) {
			this.logid = logid;
		}

		public String getLogtag() {
			return logtag;
		}

		public void setLogtag(String logtag) {
			this.logtag = logtag;
		}

		public String getAppid() {
			return appid;
		}

		public void setAppid(String appid) {
			this.appid = appid;
		}

		public String getDeviceCategory() {
			return deviceCategory;
		}

		public void setDeviceCategory(String deviceCategory) {
			this.deviceCategory = deviceCategory;
		}

		public String getPlatname() {
			return platname;
		}

		public void setPlatname(String platname) {
			this.platname = platname;
		}

		public String getFulluseridentity() {
			return fulluseridentity;
		}

		public void setFulluseridentity(String fulluseridentity) {
			this.fulluseridentity = fulluseridentity;
		}

		public String getServerid() {
			return serverid;
		}

		public void setServerid(String serverid) {
			this.serverid = serverid;
		}

		public String getRoleid() {
			return roleid;
		}

		public void setRoleid(String roleid) {
			this.roleid = roleid;
		}

		public String getGameproductid() {
			return gameproductid;
		}

		public void setGameproductid(String gameproductid) {
			this.gameproductid = gameproductid;
		}

		public int getGameproductnum() {
			return gameproductnum;
		}

		public void setGameproductnum(int gameproductnum) {
			this.gameproductnum = gameproductnum;
		}

		public String getCurrency() {
			return currency;
		}

		public void setCurrency(String currency) {
			this.currency = currency;
		}

		public int getAmount() {
			return amount;
		}

		public void setAmount(int amount) {
			this.amount = amount;
		}

		public String getRawcurrency() {
			return rawcurrency;
		}

		public void setRawcurrency(String rawcurrency) {
			this.rawcurrency = rawcurrency;
		}

		public int getRawamount() {
			return rawamount;
		}

		public void setRawamount(int rawamount) {
			this.rawamount = rawamount;
		}

		public int getGameproductamount() {
			return gameproductamount;
		}

		public void setGameproductamount(int gameproductamount) {
			this.gameproductamount = gameproductamount;
		}

		public String getGameorderid() {
			return gameorderid;
		}

		public void setGameorderid(String gameorderid) {
			this.gameorderid = gameorderid;
		}

		public String getPlatorderid() {
			return platorderid;
		}

		public void setPlatorderid(String platorderid) {
			this.platorderid = platorderid;
		}

		public int getGamecurrencytype() {
			return gamecurrencytype;
		}

		public void setGamecurrencytype(int gamecurrencytype) {
			this.gamecurrencytype = gamecurrencytype;
		}

		public int getGamecurrencynum() {
			return gamecurrencynum;
		}

		public void setGamecurrencynum(int gamecurrencynum) {
			this.gamecurrencynum = gamecurrencynum;
		}

		public int getExtragamecurrencynum() {
			return extragamecurrencynum;
		}

		public void setExtragamecurrencynum(int extragamecurrencynum) {
			this.extragamecurrencynum = extragamecurrencynum;
		}

		public String getExtra1() {
			return extra1;
		}

		public void setExtra1(String extra1) {
			this.extra1 = extra1;
		}

		public String getExtra2() {
			return extra2;
		}

		public void setExtra2(String extra2) {
			this.extra2 = extra2;
		}
	}

	@Override
	protected String csvContent() {
		return null;
	}

	@Override
	protected String csvFileName() {
		return null;
	}

	public AuanyPayorderFlow getQueryBean() {
		return queryBean;
	}

	public void setQueryBean(AuanyPayorderFlow queryBean) {
		this.queryBean = queryBean;
	}

	public List<Object[]> getResult() {
		return result;
	}

	public void setResult(List<Object[]> result) {
		this.result = result;
	}

	public Date getDateStart() {
		return dateStart;
	}

	public void setDateStart(Date dateStart) {
		this.dateStart = dateStart;
	}

	public Date getDateEnd() {
		return dateEnd;
	}

	public void setDateEnd(Date dateEnd) {
		this.dateEnd = dateEnd;
	}
}
